package util;

import java.util.List;
import java.util.Observable;
import java.util.Random;

import field.objects.Ship;

public class EnemyLoader extends Observable implements Runnable {
    private List<Ship> enemyMap;
    private Ship boss;
    private long sleepTime;

    public EnemyLoader(List<Ship> enemyMap, Ship boss, long sleepTime) {
        this.enemyMap = enemyMap;
        this.boss = boss;
        this.sleepTime = sleepTime;
    }

    @Override
    public void run() {
        try {
            while (enemyMap.size() > 0) {
                Random rnd = new Random();
                int number = rnd.nextInt(enemyMap.size());
                EnemyListSingleton.getEnemyList().getEnemies().add(enemyMap.get(number));
                enemyMap.remove(number);
                Thread.sleep(sleepTime);
            }
            if (enemyMap.isEmpty()) {
                boss.setSize(71, 77);
                EnemyListSingleton.getEnemyList().getEnemies().add(boss);
                observeSuper();
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void observeSuper() {
        setChanged();
        notifyObservers(boss);
    }
}
